﻿<script lang="ts" name="baseRoutingDetail" setup>
import { ref, reactive, onMounted } from "vue";
import { ElMessage } from "element-plus";
import type { FormRules } from "element-plus";
import { formatDate } from '/@/utils/formatTime';
import { useBaseRoutingDetailApi } from '/@/api/baseprocess/baseRoutingDetail';
import { useBaseProcessApi } from '/@/api/baseprocess/baseProcess';

//父级传递来的函数，用于回调
const emit = defineEmits(["reloadTable"]);
const baseRoutingDetailApi = useBaseRoutingDetailApi();
const baseProcessApi = useBaseProcessApi();
const ruleFormRef = ref();

const state = reactive({
	title: '',
	loading: false,
	showDialog: false,
	ruleForm: {} as any,
	stores: {},
	baseProcesses: [] as any[], // 
	dropdownData: {} as any,
});

// 自行添加其他规则
const rules = ref<FormRules>({
});

// 页面加载时
onMounted(async () => {
});

// 打开弹窗
const openDialog = async (row: any, title: string) => {
	state.title = title;
	row = row ?? {  };
	state.ruleForm = row.id ? await baseRoutingDetailApi.detail(row.id).then(res => res.data.result) : JSON.parse(JSON.stringify(row));

	// 加载数据
	state.baseProcesses = await baseProcessApi.page({ page: 1, pageSize: 1000 }).then(res => res.data.result.items);

	state.showDialog = true;
};

// 关闭弹窗
const closeDialog = () => {
	emit("reloadTable");
	state.showDialog = false;
};

// 提交
const submit = async () => {
	ruleFormRef.value.validate(async (isValid: boolean, fields?: any) => {
		if (isValid) {
			let values = state.ruleForm;
			await baseRoutingDetailApi[state.ruleForm.id ? 'update' : 'add'](values);
			closeDialog();
		} else {
			ElMessage({
				message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
				type: "error",
			});
		}
	});
};

//将属性或者函数暴露给父组件
defineExpose({ openDialog });
</script>
<template>
	<div class="baseRoutingDetail-container">
		<el-dialog v-model="state.showDialog" :width="800" draggable :close-on-click-modal="false">
			<template #header>
				<div style="color: #fff">
					<span>{{ state.title }}</span>
				</div>
			</template>
			<el-form :model="state.ruleForm" ref="ruleFormRef" label-width="auto" :rules="rules">
				<el-row :gutter="35">
					<el-form-item v-show="false">
						<el-input v-model="state.ruleForm.id" />
					</el-form-item>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="工序顺序" prop="sortNum">
							<el-input-number v-model="state.ruleForm.sortNum" placeholder="请输入工序顺序" maxlength="32" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<!-- <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="工艺路线主键" prop="routingId">
							<el-input v-model="state.ruleForm.routingId" placeholder="请输入工艺路线主键" maxlength="32" show-word-limit clearable />
						</el-form-item>
					</el-col> -->
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="工艺路线编码" prop="routingCode">
							<el-input v-model="state.ruleForm.routingCode" placeholder="请输入工艺路线编码" maxlength="32" show-word-limit clearable disabled/>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="工艺路线名称" prop="routingName">
							<el-input v-model="state.ruleForm.routingName" placeholder="请输入工艺路线名称" maxlength="32" show-word-limit clearable disabled/>
						</el-form-item>
					</el-col>
					<!-- <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="工序id" prop="processId">
							<el-input v-model="state.ruleForm.processId" placeholder="请输入工序id" maxlength="32" show-word-limit clearable />
						</el-form-item>
					</el-col> -->
					<!-- <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="工序码" prop="processCode">
							<el-input v-model="state.ruleForm.processCode" placeholder="请输入工序码" maxlength="32" show-word-limit clearable />
						</el-form-item>
					</el-col> -->
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="工序码" prop="processCode">
							<el-select v-model="state.ruleForm.processCode" placeholder="请选择工序码" clearable>
								<el-option
									v-for="process in state.baseProcesses"
									:key="process.processCode"
									:label="process.processCode"
									:value="process.processCode"
								/>
							</el-select>
						</el-form-item>
					</el-col>
					<!-- <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="工序名称" prop="processName">
							<el-input v-model="state.ruleForm.processName" placeholder="请输入工序名称" maxlength="32" show-word-limit clearable />
						</el-form-item>
					</el-col> -->
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="() => state.showDialog = false">取 消</el-button>
					<el-button @click="submit" type="primary" v-reclick="1000">确 定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>
<style lang="scss" scoped>
:deep(.el-select), :deep(.el-input-number) {
  width: 100%;
}
</style>